<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .screen{
            margin: 100px auto;
            width: 800px;
            height: 800px;
            border: 2px black solid;
        }
        .ball{
            width: 100px;
            height: 100px;
            background-color: pink;
            border-radius: 50%;
        }
    </style>
</head>
<body>
<div class = "screen">
    <div class = "ball"></div>
</div>
<script>
    /*
    *
    *
    * */

    let ball = document.querySelector(".ball")
    let x = 120 //用变量x初始化小球x坐标
    let y = 30 //用变量y初始化小球y坐标

    //初始化小球移动速率
    let vx = 9 //小球在x方向上的移动速率
    let vy = 5 //小球在y方向上的移动速率

    setInterval(function (){
        //小球各方向触碰边界
        if(x >= 700){
            vx *= -1 //小球x一旦触碰到边界 vx就变成负方向 但是现实中速度会衰减 所以要成小一点的数
        }
        if(y >= 700){
            vy *= -1 //同上面
        }
        if(x < 0){
            vx *= -1
        }
        if(y < 0){
            vy *= -1
        }

        //修改小球x和y
        x += vx
        y += vy
        //实时渲染小球坐标位置
        ball.style.transform = `translate(${x}px,${y}px)`
    },1000/60)

</script>
</body>
</html>